package cn.com.headfree.loginmes.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.com.headfree.util.RedisClusterUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.gta.edu.sdk.util.StringUtil;

import cn.com.headfree.base.controller.ExBaseController;
import cn.com.headfree.base.controller.ResultData;
import cn.com.headfree.entity.LoginMes;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.loginmes.service.ILoginMesService;


/**
 * 附件处理controller
 *
 * @author xuliang
 */
@Controller
@RequestMapping(value = "/loginMes", produces = "application/json;charset=UTF-8")
public class LoginMesController extends ExBaseController {
    private static final Logger LOG = LogManager.getLogger(LoginMesController.class);
    @Autowired
    public ILoginMesService loginMesService;

    /**
     * 获取系统在线人数
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getOnlineUsersCounts")
    @ResponseBody
    public ResultData getOnlineUsersCounts(HttpServletRequest request,HttpServletResponse response) {
        ResultData result = new ResultData();
        try {
        	// 获取redis里面存在的登陆用户数据
    		Set<String> keys = RedisClusterUtil.getInstance().keys("login-user-*");
            result.setData(keys.size());
            LOG.info("获取在线用户数成功！");
        } catch (Exception e) {
            result.setErrorMsg("获取在线用户数异常");
            result.setSuccess(false);
            LOG.error("获取在线用户数异常", e);
        }
        return result;
    }


    /**
     * 获取系统总访问次数
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getAllLoginTimes")
    @ResponseBody
    public ResultData getAllLoginTimes(HttpServletRequest request,HttpServletResponse response) {
        ResultData result = new ResultData();
        try {
        	List<LoginMes> allList=loginMesService.getAllLoginTimes();
            result.setData(allList.size());
            LOG.info("获取总访问数成功！");
        } catch (Exception e) {
            result.setErrorMsg("获取总访问数异常");
            result.setSuccess(false);
            LOG.error("获取总访问数异常", e);
        }
        return result;
    }


}
